#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int* arr = NULL;    
int capacity = 0;   
int size = 0;      
int sorted = 0;

int cmp_asc(const void* a, const void* b) {
    int x = *(const int*)a;
    int y = *(const int*)b;
    if (x < y) return -1;
    if (x > y) return 1;
    return 0;
}

int cmp_desc(const void* a, const void* b) {
    int x = *(const int*)a;
    int y = *(const int*)b;
    if (x > y) return -1;
    if (x < y) return 1;
    return 0;
}

void reverse_array() {
    int left = 0, right = size - 1;
    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}

int main() {
    int q;
    scanf_s("%d", &q); 

    capacity = 16;
    arr = (int*)malloc(capacity * sizeof(int));
    if (arr == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        return 1;
    }

    while (q--) {
        int op;
        scanf_s("%d", &op); 

        switch (op) {
        case 1: { 
            int x;
            scanf_s("%d", &x);
     
            if (size == capacity) {
                capacity *= 2;
                arr = (int*)realloc(arr, capacity * sizeof(int));
                if (arr == NULL) {
                    fprintf(stderr, "Memory reallocation failed\n");
                    return 1;
                }
            }
            arr[size++] = x;
            sorted = 0; 
            break;
        }
        case 2: 
        { 
            size--;
            sorted = 0; 
            break;
        }
        case 3:
        { 
            int i;
            scanf_s("%d", &i);
            printf("%d\n", arr[i]);
            break;
        }
        case 4: 
        { 
            int i_val, x;
            scanf_s("%d %d", &i_val, &x);
            int pos = i_val + 1; 
            if (size == capacity) {
                capacity *= 2;
                arr = (int*)realloc(arr, capacity * sizeof(int));
                if (arr == NULL) {
                    fprintf(stderr, "Memory reallocation failed\n");
                    return 1;
                }
            }
      
            for (int j = size; j > pos; j--) {
                arr[j] = arr[j - 1];
            }
            arr[pos] = x;
            size++;
            sorted = 0; 
            break;
        }
        case 5: 
        { 
            if (size <= 1) {
                sorted = 1; 
            }
            else if (sorted == 1) {
                
            }
            else if (sorted == 2) {
                reverse_array();
                sorted = 1;
            }
            else {
                qsort(arr, size, sizeof(int), cmp_asc);
                sorted = 1;
            }
            break;
        }
        case 6: 
        { 
            if (size <= 1) {
                sorted = 2; 
            }
            else if (sorted == 2) {
                
            }
            else if (sorted == 1) {
                reverse_array(); 
                sorted = 2;
            }
            else {
                qsort(arr, size, sizeof(int), cmp_desc);
                sorted = 2;
            }
            break;
        }
        case 7: 
        { 
            printf("%d\n", size);
            break;
        }
        case 8: 
        { 
            for (int i = 0; i < size; i++) {
                if (i > 0) printf(" ");
                printf("%d", arr[i]);
            }
            printf("\n");
            break;
        }

        }
    }

    free(arr); 
    return 0;
}